package game.http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

import game.module.user.bean.PlayerBean;
import game.module.user.dao.PlayerDao;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import lion.http.HttpHtmlHandler;

public class AgentHttpHandler extends HttpHtmlHandler {

	private static Logger logger = LoggerFactory.getLogger(AgentHttpHandler.class);

	private ObjectMapper objectMapper = new ObjectMapper();

	public AgentHttpHandler(String name) {
		super(name);
	}

	@Override
	public String strHttpHandle(ChannelHandlerContext ctx, FullHttpRequest request, String resString) throws Exception {
		logger.info("agent http request：{}", resString);
		Map<String, Object> reqMap = objectMapper.readValue(resString, Map.class);
		List<Integer> playerIds = (List<Integer>) reqMap.get("playerId");
		List<PlayerBean> playerBeans = PlayerDao.getInstance().getPlayerByIds(playerIds);
		// 返回数据
		Map<String, Object> retMap = new HashMap<>();
		retMap.put("module", "player");
		retMap.put("playerId", playerIds);
		List<PlayerBean4Agent> playerNewList = new ArrayList<>();
		retMap.put("ret", playerNewList);
		for (PlayerBean aPlayerBean : playerBeans) {
			PlayerBean4Agent playerBean4Agent = new PlayerBean4Agent();
			playerBean4Agent.id = aPlayerBean.getId();
			playerBean4Agent.accountId = aPlayerBean.getAccountId();
			playerBean4Agent.name = aPlayerBean.getName();
			playerBean4Agent.sex = aPlayerBean.getSex();
			playerBean4Agent.headimg = aPlayerBean.getHeadimg();
			playerBean4Agent.level = aPlayerBean.getLevel();
			playerBean4Agent.level_exp = aPlayerBean.getLevel_exp();
			playerBean4Agent.coins = aPlayerBean.getCoins();
			playerBean4Agent.diamond = aPlayerBean.getFang_ka();
			playerBean4Agent.create_time = (int) (aPlayerBean.getCreate_time().getTime() / 1000);
			playerNewList.add(playerBean4Agent);
		}
		return objectMapper.writeValueAsString(retMap);
	}

	public static final class PlayerBean4Agent {
		public Integer id;

		public String accountId;

		public String name;

		public Integer sex;

		public String headimg;

		public Integer level;

		public Integer level_exp;

		public Integer coins;

		public Integer diamond;

		public Integer create_time;
	}

}
